<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户账号" prop="userId">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电卡号" prop="cardId">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入电卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易状态" prop="tranStatus">
        <el-select v-model="queryParams.tranStatus" placeholder="请选择交易状态" clearable>
          <el-option
            v-for="dict in dict.type.tran_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="交易类型" prop="tranType">
        <el-select v-model="queryParams.tranType" placeholder="请选择交易类型" clearable>
          <el-option
            v-for="dict in dict.type.tran_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="充值日期" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:recharge_record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recharge_recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="用户ID" align="center" prop="rechargeRecordId" />-->
      <el-table-column label="用户/电卡号" align="center" prop="userName" >
        <template slot-scope="scope">
          <div v-if="scope.row.userName">{{scope.row.userName}}</div>
          <div v-else>{{scope.row.cardNo}}</div>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center" prop="cardNo" >
        <template slot-scope="scope">
          <div v-if="scope.row.userName">用户充值</div>
          <div v-else>电卡充值</div>
        </template>
      </el-table-column>
      <el-table-column label="充值方案" align="center" prop="rechargeSchemeId" width="120" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dict_recharge_scheme" :value="scope.row.rechargeSchemeId"/>
        </template>
      </el-table-column>
      <el-table-column label="交易状态" align="center" prop="tranStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tran_status" :value="scope.row.tranStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="交易类型" align="center" prop="tranType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tran_type" :value="scope.row.tranType"/>
        </template>
      </el-table-column>
      <el-table-column label="充值金额" align="center" prop="balance" />
      <el-table-column label="赠送金额" align="center" prop="give" />
      <el-table-column label="充值日期" align="center" prop="createTime" width="160"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值记录对话框 -->
  </div>
</template>

<script>
import { listRecharge_record, getRecharge_record, delRecharge_record, addRecharge_record, updateRecharge_record } from "@/api/system/recharge_record";

export default {
  name: "Recharge_record",
  dicts: ['tran_status', 'tran_type', 'dict_recharge_scheme'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值记录表格数据
      recharge_recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'recharge_record_id',
        isAsc: 'desc',
        userName: null,
        cardNo: null,
        tranStatus: null,
        tranType: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        cardId: [
          { required: true, message: "电卡号不能为空", trigger: "blur" }
        ],
        rechargeSchemeId: [
          { required: true, message: "充值方案id不能为空", trigger: "blur" }
        ],
        tranStatus: [
          { required: true, message: "交易状态不能为空", trigger: "change" }
        ],
        tranType: [
          { required: true, message: "交易类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询充值记录列表 */
    getList() {
      this.loading = true;
      listRecharge_record(this.queryParams).then(response => {
        this.recharge_recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rechargeRecordId: null,
        userId: null,
        cardId: null,
        rechargeSchemeId: null,
        tranStatus: null,
        tranType: null,
        balance: null,
        give: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rechargeRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rechargeRecordId = row.rechargeRecordId || this.ids
      getRecharge_record(rechargeRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rechargeRecordId != null) {
            updateRecharge_record(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecharge_record(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rechargeRecordIds = row.rechargeRecordId || this.ids;
      this.$modal.confirm('是否确认删除充值记录编号为"' + rechargeRecordIds + '"的数据项？').then(function() {
        return delRecharge_record(rechargeRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/recharge_record/export', {
        ...this.queryParams
      }, `recharge_record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
